﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class QL_Hang_Dien_Thoai : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataAdapter ad;
    SqlCommand cmd;
    DataTable dt = new DataTable();
    private string Connect = ConfigurationManager.ConnectionStrings["ketnoi"].ConnectionString;
    string sql = "select * from Hang_SX";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"].ToString() == "")
        {
            Response.Redirect("Trang_Chu.aspx");
        }
        else
        {
            TextBox2.Enabled = true;
            btnkLuu.Visible = false;
            btnLuu.Visible = false;
            if (!IsPostBack)
            {
                Hien_Thi();
                if (dt.Rows.Count > 0)
                {

                    TextBox2.Text = dt.Rows[0]["TenHang"].ToString();

                }
            }
        }       
        
    }
    public void Hien_Thi()
    {

        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            ad = new SqlDataAdapter(sql, con);
            ad.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else
            {
                lblerr.Text = "Không có dữ liệu";
            }
        }
        catch (Exception ex)
        {
            lblerr.Text = "Lỗi: " + ex.Message;
        }
    }
   
    protected void btnThem_Click(object sender, EventArgs e)
    {
        lblerr.Text = "";
        btnThem.Visible = false;
        TextBox2.Text = "";
        btnXoa.Visible = false;
        btnLuu.Visible = true;
        btnkLuu.Visible = true;
        Session["Them"] = 1;
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        lblerr.Text = "";
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "delete from Hang_SX where TenHang='" + TextBox2.Text + "'";
            int i;
            i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                lblerr.Text = "Đã Xóa Thành Công";
                Hien_Thi();
                if (dt.Rows.Count > 0)
                {
                    TextBox2.Text = dt.Rows[0]["TenHang"].ToString();

                }
                else
                {
                    lblerr.Text = "Không xóa được";
                }
            }
            con.Dispose();
            con.Close();

        }
        catch (Exception ex)
        {
            lblerr.Text = "Lỗi:" + ex.Message;
        }
    }
    
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            string sql = "";
            cmd = new SqlCommand();
            cmd.Connection = con;
            if (Session["Them"].ToString() == "1")
            {
                sql = "insert into Hang_SX (TenHang)" + "values('" + TextBox2.Text + "')";
            }
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            int i;
            i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                lblerr.Text = "Thực hiện thành công";
                btnThem.Visible = true;
                
                btnXoa.Visible = true;
                btnLuu.Visible = false;
                btnkLuu.Visible = false;
                Hien_Thi();
            }
            else
            {
                lblerr.Text = "Thực hiện không thành công";
            }
            con.Dispose();
            con.Close();
        }
        catch (Exception ex)
        {
            lblerr.Text = "lỗi: " + ex.Message;
        }
    }
    protected void btnkLuu_Click(object sender, EventArgs e)
    {
        btnThem.Visible = true;
        btnXoa.Visible = true;
        btnLuu.Visible = false;
        btnkLuu.Visible = false;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Hien_Thi();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idrow = GridView1.SelectedIndex;
        TextBox2.Text = GridView1.Rows[idrow].Cells[0].Text.ToString();
    }
}
